package com.newtype.meeting.client.widget;

import java.util.HashMap;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.GMeeting;
import com.newtype.meeting.client.MD5;
import com.newtype.meeting.client.MeetingConstants;
import com.newtype.meeting.shared.LoginInfo;

/**
 * 登录窗口
 * @author 汪恭泽
 *
 */
public class LoginDialog extends Dialog {
	private MeetingConstants constants = GWT.create(MeetingConstants.class);
	private TextField<String> userName;//帐户
	private TextField<String> password;//密码
	private Button login;//登录按钮
	private Status status;//状态条
	private ButtonBar buttonBar;

	public LoginDialog() {
		setHeading(constants.login());
		setFrame(true);
		setSize(540, 280);
		setIconStyle("icon-login");
		setButtons("");
		setModal(true);
		setBlinkModal(true);
		setBodyBorder(false);
		setResizable(false);
		setLayout(new BorderLayout());
		//west
		createWestPanel();
		//center
		createCenterPanel();
	}

	/**
	 * west 登录面板
	 */
	private void createCenterPanel() {
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 300);
		data.setMargins(new Margins(0, 1, 0, 0));

		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.setFrame(true);
		panel.setLayout(new FormLayout());

		FormPanel form = new FormPanel();
		form.setHeaderVisible(false);
		form.setStyleAttribute("margin", "5px 20px 0px 20px");
		form.setButtonAlign(HorizontalAlignment.CENTER);
		form.setBodyBorder(false);

		Text txt1 = new Text(constants.loginNoteCn());
		form.add(txt1, new FormData("100%"));

		Text txt2 = new Text(constants.loginNoteEn());
		txt2.setStyleAttribute("margin-bottom", "15px");
		form.add(txt2, new FormData("100%"));

		//数据校验监听器
		KeyListener keyListener = new KeyListener() {
			@Override
			public void componentKeyUp(ComponentEvent event) {
				validate();
			}
		};

		//回车进行登录监听器
		KeyListener enterListener = new KeyListener() {
			@Override
			public void componentKeyPress(ComponentEvent event) {
				if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
					onSubmit();
				}
			}
		};
		//帐号输入框
		userName = new TextField<String>();
		userName.setMinLength(4);
		userName.setFieldLabel(constants.account());
		userName.addKeyListener(keyListener);
		form.add(userName, new FormData("100%"));

		//密码输入框
		password = new TextField<String>();
		password.setMinLength(4);
		password.setPassword(true);
		password.setFieldLabel(constants.password());
		password.addKeyListener(keyListener);
		password.addKeyListener(enterListener);
		form.add(password, new FormData("100%"));
		// 帐户输入框获得焦点
		setFocusWidget(userName);

		//按钮工具条
		buttonBar = new ButtonBar();

		status = new Status();
		status.setBusy("請稍等...");
		status.hide();
		status.setAutoWidth(true);
		buttonBar.add(status);
		buttonBar.add(new FillToolItem());
		//登录按钮
		login = new Button("LOGIN");
		login.disable();
		login.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				onSubmit();
			}
		});
		buttonBar.add(login);
		form.add(buttonBar);

		final VerticalPanel vp = new VerticalPanel();
		vp.setStyleAttribute("margin", "0px 30px 10px 30px");

		Html line = new Html(
				"<hr size='1' style='border:dashed 1px #C3C5C0;width:230px;'>");
		vp.add(line);

		LoginInfo loginInfo = Registry.get(GMeeting.LOGININFO);
		if (loginInfo.getLoginUrl() != null) {
			Html loginLink = new Html("<div class='nav'><a href='"
					+ loginInfo.getLoginUrl()
					+ "'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使用Google帐户登录</a></div>");
			loginLink.setStyleAttribute("margin-top", "15px");
			vp.add(loginLink);
		} else {
			Html logoutLink = new Html("<div class='nav'><a href='"
					+ loginInfo.getLogoutUrl() + "'>退出</a></div>");
			logoutLink.setStyleAttribute("margin-top", "15px");
			vp.add(logoutLink);
		}

		//添加控件
		panel.add(form);
		panel.add(vp);
		add(panel, data);
	}

	/**
	 * center 注册面板
	 */
	private void createWestPanel() {
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.setFrame(true);

		FormPanel form = new FormPanel();
		form.setHeaderVisible(false);
		form.setButtonAlign(HorizontalAlignment.CENTER);
		form.setStyleAttribute("margin", "10px 0px 0px 0px");
		form.setBodyBorder(false);

		Text txt3 = new Text(constants.registTitle());
		Text txt4 = new Text(constants.registNoteCn());
		txt4.setStyleAttribute("margin", "10px 0px 5px 0px");
		Text txt5 = new Text(constants.registNoteEn());

		form.add(txt3);
		form.add(txt4);
		form.add(txt5);

		String applyBtn = "<span style='font-size: 20px;font-weight: bold;color: #509A53;'>"
				+ constants.applyAccount() + "</span>";
		Button button = new Button(applyBtn);
		button.setSize(120, 40);
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Window window = new RegistrationWindow(new LoginInfo());
				window.show();
			}
		});
		form.addButton(button);
		panel.add(form);
		add(panel, data);
	}

	/**
	 * 提交
	 */
	protected void onSubmit() {
		status.show();
		HashMap<String, String> loginInfo = new HashMap<String, String>();
		loginInfo.put("userName", userName.getValue());
		loginInfo.put("password", MD5.MD5Encode(password.getValue()));
		Dispatcher.forwardEvent(AppEvents.CheckLogin, loginInfo);
		buttonBar.disable();
	}

	/**
	 * 重置
	 */
	public void reset() {
		buttonBar.enable();
		status.hide();
		userName.reset();
		password.reset();
		validate();
		userName.focus();
	}

	protected boolean hasValue(TextField<String> field) {
		return field.getValue() != null && field.getValue().length() > 0;
	}

	protected void validate() {
		login.setEnabled(hasValue(userName) && hasValue(password)
				&& password.getValue().length() > 3);
	}
}
